package com.xjwlmq.dx.dao.impl;

import com.xjwlmq.dx.dao.RainQualityBaseDao;
import com.xjwlmq.dx.entity.RainQuality;
import com.xjwlmq.dx.util.BaseDao;

import java.sql.SQLException;
import java.util.ArrayList;

/**
 * @ClassName RainQualityImpl
 * @Description TODO:
 * @Author 12994
 * @Date 2024/9/30 16:38
 * @Version 1.0
 **/
public class RainQualityImpl extends BaseDao implements RainQualityBaseDao {


    @Override
    public ArrayList<RainQuality> getRainQualityList() {
        ArrayList<RainQuality> rainQualities = new ArrayList<>();
        RainQuality thisQuality = null;
        try {
            this.rs = baseQuery("select RainQuality.id, DistrictName, " +
                    "Rain, MonitoringStation, MonitoringAddress, " +
                    "MonitorTime, StationType, StationName " +
                    "from RainQuality, MonitoringStationName " +
                    "where RainQuality.MonitoringStation=MonitoringStationName.id;");
            if (this.rs == null) {
                throw new NullPointerException("查询结果为空!");
            }
            while (this.rs.next()) {
                thisQuality = new RainQuality();
                thisQuality.setId(this.rs.getInt("RainQuality.id"));
                thisQuality.setRain(this.rs.getInt("Rain"));
                thisQuality.setDistrictName(this.rs.getString("DistrictName"));
                thisQuality.setStationName(this.rs.getString("StationName"));
                thisQuality.setMonitoringAddress(this.rs.getString("MonitoringAddress"));
                thisQuality.setMonitorTime(this.rs.getString("MonitorTime"));
                thisQuality.setStationType(this.rs.getInt("StationType"));
                rainQualities.add(thisQuality);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return rainQualities;
    }

    @Override
    public int delRainQualityViaId(int id) {
        return baseUpdate("DELETE FROM RainQuality WHERE id=?;", id);
    }

    @Override
    public int addRainQuality(RainQuality rainQuality) {
        return baseUpdate("INSERT INTO RainQuality (id, DistrictName, Rain, " +
                        "MonitoringStation, MonitoringAddress, MonitorTime, StationType) VALUES " +
                        "(null,?,?,?,?,?,?);",
                rainQuality.getDistrictName(),
                rainQuality.getRain(),
                rainQuality.getStationName(),
                rainQuality.getMonitoringAddress(),
                rainQuality.getMonitorTime(),
                rainQuality.getStationType());
    }

    @Override
    public int updateRainQuality(RainQuality rainQuality) {
        return baseUpdate("UPDATE RainQuality SET " +
                        "DistrictName=?,Rain=?,MonitoringStation=?,MonitoringAddress=?," +
                        "MonitorTime=?,StationType=?  " +
                        "where id=?;", rainQuality.getDistrictName(),
                rainQuality.getRain(),
                rainQuality.getStationName(),
                rainQuality.getMonitoringAddress(),
                rainQuality.getMonitorTime(),
                rainQuality.getStationType(),
                rainQuality.getId());
    }

    @Override
    public RainQuality getRainQualityViaId(int id) {
        RainQuality thisQuality = null;
        try {
            this.rs = baseQuery("SELECT * FROM RainQuality WHERE id=? LIMIT 1;", id);
            if (this.rs == null || !this.rs.next()) {
                throw new NullPointerException("查询结果为空!");
            }
            thisQuality = new RainQuality();
            thisQuality.setId(this.rs.getInt("id"));
            thisQuality.setRain(this.rs.getInt("Rain"));
            thisQuality.setDistrictName(this.rs.getString("DistrictName"));
            thisQuality.setStationName(this.rs.getString("MonitoringStation"));
            thisQuality.setMonitoringAddress(this.rs.getString("MonitoringAddress"));
            thisQuality.setMonitorTime(this.rs.getString("MonitorTime"));
            thisQuality.setStationType(this.rs.getInt("StationType"));
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return thisQuality;
    }
}
